;(function($){
	$.fn.spa_sel_jdInfo = function(opts){
		var el = this;
		var opt = {
			bmh : "",
			ds : {},
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.initValue();
		this.getMarket();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//关闭
			el.find("#modalBtnCancel,#close-dialog").unbind("click").click(function(){
				param.closeBack();
				el.modal("hide");
				el.empty();
			})
			
			//输入接待号
			el.find("#jdh_btn").unbind("click").click(function(){
				//计算器
				var status = false;
				el.find("#modal-dialog").hide();
				if(!$.isFunction($().wechatAlipayPay)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true });
				}
				el.find("#mykeyboard").digitalCalculator({
					number:el.find("#pay").val()?el.find("#pay").val():1,//数量
					status:status?status:false,
					title:param.title?param.title:"",
					//初始化数据返回回调
					dataBack:function(number){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
 						el.find("#jdh").val(number.replace(".00",""));
					},
					returnBack :function(){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
					}
				});
			});
			
			//选择公司
			el.find("#company_btn").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					if(!$.isFunction($().selection_fromProfileCompany)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/openSpa/jquery.selection_fromProfileCompany.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).selection_fromProfileCompany({
						condition:{ho_profile_where:{search:""}},//获取数据源条件json el.find("span[id=company]").text()
						dataBack : function(data){
							el.find("#company").val(data.gname||"");
							el.find("#pno").val(data.pno||"");
						},
						closeBack : function(){
							
						},
					});
				});
			})
			
			//确认按钮
			el.find("#modalBtnSave").unbind("click").click(function(){
				var json = sysname2json(el.find("#frm").serializeArray());
				param.dataBack(json);
				el.modal("hide");
				el.empty();
			})
			
			//清空
			el.find("#modalBtnClear").unbind("click").click(function(){
				param.dataBack({});
				el.modal("hide");
				el.empty();
			})
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="mykb-box" id="mykeyboard"></div>'+
						'<div id="modal-dialog" class="modal-dialog" style="width:600px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp("接待信息")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="min-height: 100px;">'+
									'<form id="frm" class="form-horizontal">'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("接待号:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" style="width:245px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="jdh" id="jdh" value="">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="jdh_btn">'+
															'<span class="ace-icon fa fa-keyboard-o icon-on-right bigger-110"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("公司:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" style="width:360px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="company" id="company">'+
													'<input type="text" class="form-control search-query input_all hide" placeholder="" name="pno" id="pno">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="company_btn">'+
															'<span class="ace-icon fa fa-keyboard-o icon-on-right bigger-110"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("市场:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group">'+// style="width:360px;"
													'<span class="input-icon input-icon-right">'+
														'<select id="market" name="market" style="height:34px;width:180px;border-radius:3px !important;"></select>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("备注:")+'</label>'+
											'<div class="col-sm-9">'+
												'<span class="input-icon input-icon-right">'+
													'<textarea class="form-control search-query" cols="50" rows="5" placeholder="" name="remark" id="remark"></textarea>'+
												'</span>'+
											'</div>'+
										'</div>'+
									'</form>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnClear" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-trash"></i>'+
													$.i18n.propJsp("清空")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnSave" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+
													$.i18n.propJsp("确认")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-times"></i>'+
													$.i18n.propJsp("返回")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		initValue : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			for(key in param.ds){
				el.find("#"+key).val(param.ds[key]);
			}
		},
		getMarket : function(){
    		var el = this.el;
			var param = this.param;
			var t = this;
			
    		var arg={"arg":{"arg":"市场","bmh":param.bmh}}
    		el.find("#market").html('<option value=""></option>');
			var output={};
            output={"page":"1","sord":"asc","rows":"-1","sidx":"mc"};
			$.ajax({
				url: $.HSAPI_AJAXURL+$.HSAPI_PROCEDURE2_LIST,
				type: "post",
				dataType: "json",
				headers : $.HSAPI_HEADERS,
				data: $.HSAPI_ASKJSONINFO(JSON.stringify(arg),'POSArgDictDefaultBybmh',JSON.stringify(output),"list"),
				success : function(data){
		    		$(data).each(function(k,v){
		    			if(v.grou_name=="市场"){
		    				var html='<option value="'+v.code+'" >'+v.descript1+'</option>'
		    				el.find("#market").append(html)
						}
		    		})
				},
				error : function(data){
					$.HSAPI_ALERT(JSON.stringify(data))
				}
			});
		},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
			
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
	}
	
	function getContextPath(){
		var pathName = document.location.pathname;
		var index = pathName.substr(1).indexOf("/");
		var path = pathName.substr(0, index + 1);
		return path;
	}
})(jQuery);